package jwdt.xadmin.system.service.impl;

import jwdt.common.dao.BaseDao;
import jwdt.xadmin.system.bean.TUser;
import jwdt.xadmin.system.service.UserManageService;

import com.ezerg.db.Jdb;
import com.ezerg.db.ext.PageUtil;
import com.ezerg.util.DateUtil;
import com.ezerg.util.StringUtil;

public class UserManageServiceImpl implements UserManageService{
	
	private BaseDao<TUser> baseDao;
	
	public void setBaseDao(BaseDao<TUser> baseDao) {
		this.baseDao = baseDao;
	}
	
	@Override
	public PageUtil doListUser(PageUtil pu,TUser user) throws Exception{
		String where = " where 1=1 ";
		String userName = user.getUsername();
		String fullName = user.getFullname();
		String osKey = user.getOskey();
		Integer depid = user.getDepid();
		if(!StringUtil.nullOrBlank(userName)){
			where +=  " and username like '%" + userName +"%'";
		}
		if(!StringUtil.nullOrBlank(fullName)){
			where += " and fullname like '%"+ fullName + "%'";
		}
		if(!StringUtil.nullOrBlank(osKey)){
			where += " and oskey = '" + osKey + "'";
		}
		if(depid != null && depid != 0) {
			where += " and depid=" + depid;
		}
		String querySQL = "select userid,fullname,jinyong,lastlogin,number,oskey,(SELECT jsmc FROM jwdt_qx_js WHERE jsdm=oskey) AS oskeyname,(SELECT NAME FROM t_dep WHERE id=depid) AS depname,shenhe,tel,useremail,userlogins,username from t_user"+ where +" order by userid asc";
		pu.doList(querySQL);
		return pu;
	}
	
	@Override
	public String doAddact(TUser obj) throws Exception {
		if(obj.getDepid()==0){
			obj.setDepid(null);
		}
		obj.setUserpassword(StringUtil.MD5(obj.getUserpassword()));
		obj.setJoindate(DateUtil.getDateTime());
		obj.setJinyong(1);
		obj.setUsersex("0");
		obj.setUserbirthday(DateUtil.getDate());
		return baseDao.doAddObj(obj);
	}

	@Override
	public String doEditact(TUser obj, int id) throws Exception {
		if(obj.getDepid()!=null &&obj.getDepid() ==0){
			obj.setDepid(null);
		}
		TUser obj2 = (TUser)Jdb.get().get(TUser.class, id);
		obj.setUserid(id);
		if(!obj.getUserpassword().equals(obj2.getUserpassword())) {
			obj.setUserpassword(StringUtil.MD5(obj.getUserpassword()));
		} else {
			obj.setUserpassword(null);
		}
		return baseDao.doUpdateObj(TUser.class, id, obj);
	}
	
	@Override
	public String doDelact(int id) throws Exception {
		return baseDao.doDelObj(TUser.class, id);
	}

	@Override
	public TUser doLoad(int id) throws Exception {
		return baseDao.doLoadObj(TUser.class, id);
	}
	
	@Override
	public String doUpdateState(int id,String result) throws Exception {
		return baseDao.doUpdateState(TUser.class, "userid="+id, result);
	}
}
